package com.life.mapper;

import com.github.pagehelper.Page;
import com.life.annotation.AutoFill;
import com.life.enumeration.OperationType;
import com.life.pojo.dto.FileQueryDTO;
import com.life.pojo.entity.File;
import com.life.pojo.vo.FileVO;
import org.apache.ibatis.annotations.*;

@Mapper
public interface FileMapper {

    @Insert("INSERT INTO file(name, path,type, width, height, size,file_md5,first_256KB_md5,create_time,update_time) VALUES (#{name}, #{path},#{type}, #{width}, #{height}, #{size},#{fileMD5},#{first256KBMD5}, #{createTime},#{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @AutoFill(OperationType.INSERT)
    void save(File file);

    @Select("SELECT * FROM file WHERE path LIKE CONCAT('%',#{type},'%') ORDER BY id DESC LIMIT 1")
    File findLast(String type);

    @Select("SELECT * FROM file WHERE type = #{type} ORDER BY update_time DESC")
    Page<FileVO> list(FileQueryDTO fileQueryDTO);

    @Select("SELECT * FROM file WHERE size = #{size} AND file_md5 = #{fileMD5} AND first_256KB_md5 = #{first256KBMD5} AND type = #{type}")
    File getByCondition(@Param("size") Long size, @Param("fileMD5") String fileMD5, @Param("first256KBMD5") String first256KBMD5, @Param("type") String type);

    @Delete("DELETE FROM file WHERE id = #{id}")
    void delete(Long id);

    void update(File file);
}
